import 'package:flutter/material.dart';
// 知识点: 页面布局、路由参数渲染、动态背景图片、路由传参Navigator.pop

// 创建有状态Widget Weather

class Weather extends StatefulWidget {
  const Weather({super.key});

  @override
  State<Weather> createState() => _WeatherState();
}

class _WeatherState extends State<Weather> {
  Map params = {};
  bool ispop = false;
  @override
  Widget build(BuildContext context) {
    // 获取路由传过来的参数 区别pop过来的情况
    if (!ispop) {
      params = ModalRoute.of(context)!.settings.arguments as Map;
    }
    print('params: $params');
    // 根据是否是白天动态选择背景图片
    String bgImg = params["isDaytime"] ? "light.jpg" : "dark.jpg";
    Color bgColor = params["isDaytime"] ? Colors.blue : Colors.indigo;
    return Scaffold(
      backgroundColor: bgColor,
      body: SafeArea(
        child: Container(
          // 设置背景图片
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("assets/images/$bgImg"),
              fit: BoxFit.cover
            )
          ),
          child: Padding(
            padding: const EdgeInsets.fromLTRB(0, 200, 0, 0),
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    // 带定位图标带按钮,按钮提示文案是“Edite Location”
                    Icon(
                      Icons.location_on,
                      size: 20.0,
                      color: params["isDaytime"] ? Colors.grey[800] : Colors.grey[300],
                    ),
                    TextButton(
                      onPressed: () async {
                        // 跳转到ChooseLocation页面
                        dynamic result = await Navigator.pushNamed(context, "/location");
                        print("result = $result");
                        if (result != null) {
                          setState(() {
                            // 更新数据
                            params = result;
                            ispop = true;
                          });
                        }
                      },
                      child: Text(
                        "Edite Location",
                        style: TextStyle(
                          color: params["isDaytime"] ? Colors.grey[800] : Colors.grey[300],
                        ),
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 15.0,
                ),
                // 天气
                Text(
                    params["location"],
                    style: TextStyle(
                        fontSize: 30.0,
                        color:  params["isDaytime"] ? Colors.grey[900] :  Colors.grey[300],
                    )
                ),
                SizedBox(
                  height: 20.0,
                ),
                // 参数
                Text(
                    params["time"],
                    style: TextStyle(
                        fontSize: 50.0,
                        color:  params["isDaytime"] ? Colors.grey[900] : Colors.grey[200],
                        fontWeight: FontWeight.bold,
                        fontFamily: "Caveat"
                    )
                ),
              ],
            ),
          ),
        )
      ),
    );
  }
}
